public class MySingleList implements IList{

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void creatList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        head = node1;

        display();
//        System.out.println(size());
        addFirst(22);
        display();
    }

    @Override
    public void addFirst(int data) {

        ListNode p = new ListNode(data);
        p.next = head;
        head = p;
    }

    @Override
    public void addLast(int data) {

        ListNode p = new ListNode(data);
        ListNode t = head;
        while(t.next != null) {
            t = t.next;
        }
        t.next = p;
    }

    @Override
    public void addIndex(int index, int data) {

        int len = size();
        if(index < 0 || index > len) {
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len) {
            addLast(data);
            return;
        }
        ListNode t = new ListNode(data);
        ListNode p = head;
        while((index - 1) != 0) {
            p = p.next;
            index--;
        }
        t.next = p.next;
        p.next = t;
    }

    @Override
    public boolean contains(int key) {
        ListNode p = head;
        while(p != null) {
            if(p.val == key) {
                return true;
            }
            p = p.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {

        if(head == null) {
            return;
        }
        if(key == head.val) {
            head = head.next;
            return;
        }
        ListNode p = head;
        while(p.next != null) {
            if(p.next.val == key) {
                p.next = p.next.next;
                return;
            }
            p = p.next;
        }
        return;
    }

    @Override
    public void removeAllKey(int key) {

        if(head == null) {
            return;
        }
        ListNode p = head;
        ListNode t = head.next;
        while(t != null) {
            if(t.val == key) {
                p.next = t.next;
                t = t.next;
            } else {
                p = p.next;
                t = t.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }

    private ListNode findNodeOfKey(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public int size() {
        int count = 0;
        ListNode p = head;
        while(p != null) {
            count++;
            p = p.next;
        }
        return count;
    }

    @Override
    public void clear() {

        if(head == null) {
            return;
        }
        ListNode p = head;
        while (p != null) {
            ListNode t = p.next;
            p.next = null;
            p = t;
        }
        head = null;
    }

    @Override
    public void display() {

        ListNode p = head;
        while(p != null) {
            System.out.print(p.val + " ");
            p = p.next;
        }
        System.out.println();
    }

    public void reverse() {
        if(head == null) {
            return;
        }
        ListNode p = head.next;
        ListNode t = p.next;
        head.next = null;
        p.next = head;
        head = p;
        p = t;
        while(p != null) {
            t = p.next;
            p.next = head;
            head = p;
            p = t;
        }
    }

    public int middlenode() {
        if(head == null) {
            return -1;
        }

        ListNode p = head;
        ListNode t = head;
        while (p != null && p.next != null) {
            p = p.next.next;
            t = t.next;
        }
        return t.val;
    }

    public ListNode kthToLast(int k) {
        if(head == null) {
            return null;
        }

        if(k <= 0) {
            return null;
        }
        ListNode p = head;
        ListNode t = head;

        while ((k - 1) != 0) {
            t = t.next;
            if(t == null) {
                return null;
            }
            k--;
        }
        while (t.next != null) {
            p = p.next;
            t = t.next;
        }
        return p;
    }
}
